package com.sense.kukoo.biz_explore.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.*
import com.sense.kukoo.lib_logger.LoggerManager

//结束加载更多
const val ACTION_FINISH_LOAD_MORE = 10001

class ContentViewModel : BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    val exploreContentData:MutableLiveData<ArrayList<ExploreContentBean>> = MutableLiveData()
    val enableLoadMore:MutableLiveData<Boolean> = MutableLiveData(true)
    var mCode:String?=null

    private var mIndex = 2
    private val mPageSize = 10
    private var lastNetworkAction:Int = TYPE_RECOMMEND
    //未添加第一个1xn
    private var flag_firstBook_1xn:Boolean = true

    companion object{
        val TYPE_RECOMMEND = 0
        val TYPE_MORE = 1
    }

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun getRecommendData() {
        lastNetworkAction = TYPE_RECOMMEND
        request {
            val result = Repo.http.getRecommendData(mCode!!)
            if (result.success) {
                val data: List<RecommendBean> = result.data!!
                flag_firstBook_1xn = true
                mIndex = 2
                val formateData = formateRecommendData(data)
                exploreContentData.postValue(formateData)
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun getMoreData(){
        lastNetworkAction = TYPE_MORE
        request {
            val result = Repo.http.getMoreData(mIndex, mPageSize, mCode!!)
            if (result.success) {
                val data: QueryPageBean<BookBean> = result.data!!
                val formate = formatePageResultOfBookBean(data)
                //
                LoggerManager.d(TAG,"getMoreData -> success")
                mIndex++
                if(data.last){
                    LoggerManager.d(TAG,"ACTION_NO_MORE_DATA data.records.size=${data.records.size}")
                    enableLoadMore.postValue(false)
                }
                val value = exploreContentData.value
                value!!.addAll(formate)
                exploreContentData.postValue(value)
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_FINISH_LOAD_MORE)
        }
    }

    //网络出错时的重试
    fun retryNetwork(){
        when (lastNetworkAction) {
            TYPE_RECOMMEND -> {
                getRecommendData()
            }
            TYPE_MORE -> {
                getMoreData()
            }
        }
    }

    private fun formateRecommendData(data :List<RecommendBean>):ArrayList<ExploreContentBean>{
        val formate = arrayListOf<ExploreContentBean>()
        data.forEach { bean ->
            if(bean.books.size > 0){
                when (bean.params) {
                    RecommendBean.PARAMS_2X3 -> {
                        val type2X3 = ExploreContentBean(ExploreContentBean.TYPE_2_X_3)
                        type2X3.books = bean.books
                        if(bean.total <= 6){
                            type2X3.code = null
                        }else{
                            type2X3.code = bean.code
                        }
                        type2X3.title = bean.title
                        formate.add(type2X3)
                    }
                    RecommendBean.PARAMS_3X2 -> {
                        val type3X2 = ExploreContentBean(ExploreContentBean.TYPE_3_X_2)
                        type3X2.books = bean.books
                        if(bean.total <= 6){
                            type3X2.code = null
                        }else{
                            type3X2.code = bean.code
                        }
                        type3X2.title = bean.title
                        formate.add(type3X2)
                    }
                    RecommendBean.PARAMS_1XN -> {
                        val size = bean.books.size
                        if(size<mPageSize){
                            enableLoadMore.postValue(false)
                        }else{
                            enableLoadMore.postValue(true)
                        }
                        bean.books.forEach {
                            val type1XN = ExploreContentBean(ExploreContentBean.TYPE_1_X_N)
                            type1XN.book = it
                            if (flag_firstBook_1xn) {
                                flag_firstBook_1xn = false
                                type1XN.isFirst = true
                                type1XN.title = bean.title
                            }
                            formate.add(type1XN)
                        }
                    }
                }
            }
        }
        return formate
    }

    fun formatePageResultOfBookBean(data:QueryPageBean<BookBean>):List<ExploreContentBean>{
        val formate = arrayListOf<ExploreContentBean>()
        data.records.forEach {
            val type1XN = ExploreContentBean(ExploreContentBean.TYPE_1_X_N)
            type1XN.book = it
            formate.add(type1XN)
        }
        return formate
    }

}